/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.edu.pucp.cpnc.mm.view;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import pe.edu.pucp.cpnc.mm.bean.Map;
import pe.edu.pucp.cpnc.mm.manager.CmdManager;
import pe.edu.pucp.cpnc.mm.renderer.CameraPanController;

/**
 *
 * @author
 */
public class MapViewerPanel extends javax.swing.JPanel{
    private Map map;
    protected double mouseX;
    protected double mouseY;
    private final MainEventHandler evtHandler;
    private Timer timer;
    
    @SuppressWarnings("LeakingThisInConstructor")
    public MapViewerPanel(Map receivedMap) {       
        evtHandler = new MainEventHandler(this);
        this.addMouseListener(evtHandler);
        this.addKeyListener(evtHandler);
        this.addFocusListener(evtHandler);
        this.addMouseWheelListener(evtHandler);
        this.setFocusable(true);
        setLayout(new BorderLayout());
        this.map = receivedMap;
        this.setDoubleBuffered(true);
        startLoop();
        initComponents();
    }
    
    private void startLoop(){
        timer = new Timer(1000/30, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                ((MinimapPanel)(MainFrame.miniMapPane)).updateCamera();
                update();
            }
        });
        timer.start();
    }
	
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        int j = MainFrame.getIsoX(mouseX,mouseY);
        int i = MainFrame.getIsoY(mouseX, mouseY);
        MainFrame.rd.printMapNew(map, g, this, j, i,
            MainFrame.selObj, MainFrame.selectedOrientation);
        lblToolTip.repaint();
    }
    
    public Map getMap(){
        return map;
    }
    
    public void setMap(Map sMap){
        map = sMap;
    }

    public void changeToolTipText(String text){
        lblToolTip.setText(text);
    }
    
    public void update(){
        Point point = MouseInfo.getPointerInfo().getLocation();
        Point point2;
        try{point2 = getLocationOnScreen();}
        catch(Exception e){point2 = new Point(0, 0);}
        
        mouseX = point.getX() - point2.getX();
        mouseY = point.getY() - point2.getY();

        int j = MainFrame.getIsoX(mouseX,mouseY);
        int i = MainFrame.getIsoY(mouseX, mouseY);
        
        if(evtHandler.mouseRClickPressed() && MainFrame.selectedTile!=null){
            for(int i1= i-MainFrame.selectedBrushSize+1; i1<=i+MainFrame.selectedBrushSize-1; i1++){
                for(int j1= j-MainFrame.selectedBrushSize+1; j1<=j+MainFrame.selectedBrushSize-1; j1++){
                    CmdManager.changeTile(j1, i1, MainFrame.selectedTile ,map);
                    MainFrame.rd.updateMinimapTile(map, i1, j1, MainFrame.miniMapPane);
                }
            }
            
        }
        CameraPanController.updateCamera();
        repaint();
        MainFrame.miniMapPane.repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblToolTip = new javax.swing.JLabel();

        lblToolTip.setFont(new java.awt.Font("Gill Sans MT", 0, 18)); // NOI18N
        lblToolTip.setForeground(new java.awt.Color(255, 255, 255));
        lblToolTip.setToolTipText("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblToolTip, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 300, Short.MAX_VALUE)
                .addComponent(lblToolTip))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lblToolTip;
    // End of variables declaration//GEN-END:variables
}
